﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace Apps.Yunwei.Views
{
    public partial class ColorSelector : ChildWindow
    {
        public ColorSelector( int mode)
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ColorSelector_Loaded);
            SelectorMode = mode;
        }
        public int SelectorMode { get; set; }
        string bgc = string.Empty;
        void ColorSelector_Loaded(object sender, RoutedEventArgs e)
        {
            dynamic data = this.DataContext;
            if (SelectorMode == 1)
            {
                bgc = data.Bgc;

                list.SetBinding(ListBox.SelectedItemProperty, new Binding()
                {
                    Source = this.DataContext,
                    Path = new PropertyPath("Bgc"),
                    Mode = BindingMode.TwoWay
                });
            }
            else if (SelectorMode == 2)
            {
                bgc = data.Frc;

                list.SetBinding(ListBox.SelectedItemProperty, new Binding()
                {
                    Source = this.DataContext,
                    Path = new PropertyPath("Frc"),
                    Mode = BindingMode.TwoWay
                });
            }
            RequestColors();
        }
        // 请求颜色清单
        private void RequestColors()
        {
            Q.ConfigManager.RequestColors((success, actionArgs) =>
            {
                if (success == false) return;

                list.ItemsSource = actionArgs;
                if (actionArgs.Any(p => p == bgc))
                {
                    list.SelectedValue = bgc;
                }
            });
        }
        
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            dynamic data = this.DataContext;
            if (SelectorMode == 1)
            {
                data.Bgc = bgc;
            }
            else if (SelectorMode == 2)
            {
                data.Frc = bgc;
            }
            this.DialogResult = false;
        }
    }
}
